import {createRouter, createWebHistory} from 'vue-router'
import pinia from '@/store/storeRegister.js'
import { userinfo } from '@/store/index.js'
import {menuListStore} from '@/store/menuList.js'
import { ElMessage } from 'element-plus'
const login = () => import("@/view/login/index.vue")
const register = () => import("@/view/register/index.vue")
export const routes = [
    {   
        path: '/login',
        component:login,
    },
    {   
        path: '/register',
        component:register,
    },
    {
        path: '/:cathchAll(.*)',
        name: '404',
        component: () => import('@/components/404.vue')
      },

];
const router = createRouter({
    history: createWebHistory(), 
    routes
})

router.beforeEach(async (to, from, next) => {
  const userinfoStore = userinfo(pinia)
  const token = userinfoStore.profile.token
  if(to.path.startsWith('/page')&&!token) {
    ElMessage({
        message:'您还没有完成登录！请登录！',
        type:'warning'
    })
    next({
        path:'/login'
    })
  }
  next()
})

export default router